// 颜色分类
// https://leetcode.cn/problems/sort-colors/description/

/**
示例 1：

输入：nums = [2,0,2,1,1,0]
输出：[0,0,1,1,2,2]
示例 2：

输入：nums = [2,0,1]
输出：[0,1,2]
 */

/**
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var sortColors = function (nums) {
    let zeroCount = 0;
    let twoCount = 0;

    console.log(`before: ${JSON.stringify(nums)}`);

    for (let i = 0; i < nums.length; i++) {
        if (nums[i] !== 1) {
            if (nums[i] === 0) {
                zeroCount += 1;
            } else {
                twoCount += 1;
            }
            // nums.splice(i, 1);
            // i = i - 1;
        }
    }

    for (let i = 0; i < zeroCount; i++) {
        nums[i] = 0;
    }

    for (let i = zeroCount; i < nums.length - twoCount; i++) {
        nums[i] = 1;
    }

    for (let i = nums.length - twoCount; i < nums.length; i++) {
        nums[i] = 2;
    }

    console.log(`after: ${JSON.stringify(nums)}`);
};
sortColors([2, 0, 2, 1, 1, 0]);
sortColors([2, 0, 1]);
